package com.zty.prince.bdmapforlf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MapView mapView = null;
    private BroadcastReceiver receiver;
    private LinearLayout llfunc;
    private Button zi;
    private Button zo;
    private Button ro;
    private Button ov;
    private Button mo;
    private Button showfunc;
    private MapStatusUpdate mapStatusUpdate;
    private BaiduMap mapcontroller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.bmapView);
        registerSDKInitReceiver();
//        1.	隐藏缩放按钮、比例尺
        mapView.showScaleControl(true);//        设置是否显示比例尺控件


        mapView.showZoomControls(true);//        设置是否显示缩放控件


//        2.	获取获取最小（3）、最大缩放级别（20）
        //获取BaiduMap，相当于mvc mapview是视图层 BaiduMap是控制层
        mapcontroller = mapView.getMap();
        Log.d("缩放级别的最大最小", mapcontroller.getMinZoomLevel()+"~"+ mapcontroller.getMaxZoomLevel());

//       3.	设置地图中心点为修正校区
        mapStatusUpdate = MapStatusUpdateFactory.newLatLng(new LatLng(39.579527, 116.757455));

        mapcontroller.setMapStatus(mapStatusUpdate);

//        4.	设置地图缩放为15
        mapStatusUpdate = MapStatusUpdateFactory.zoomTo(20);

        mapcontroller.setMapStatus(mapStatusUpdate);

//        5.	更新地图状态
        llfunc = (LinearLayout) findViewById(R.id.allfunction);
        showfunc = (Button) findViewById(R.id.showfunc);

        zi = (Button) findViewById(R.id.zoomin);
        zo = (Button) findViewById(R.id.zoomout);
        ro = (Button) findViewById(R.id.rotate);
        ov = (Button) findViewById(R.id.overlook);
        mo = (Button) findViewById(R.id.move);

        showfunc.setOnClickListener(this);
        zi.setOnClickListener(this);
        zo.setOnClickListener(this);
        ro.setOnClickListener(this);
        ov.setOnClickListener(this);
        mo.setOnClickListener(this);



//        1)	缩小
//        2)	放大
//        3)	旋转（0 ~ 360），每次在原来的基础上再旋转30度
//        4)	俯仰（0 ~ -45），每次在原来的基础上再俯仰-5度
//        5)	移动
//        6.	获取地图Ui控制器：隐藏指南针


    }

    //百度地图的广播接受者，这是一个广播
    private void registerSDKInitReceiver() {
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(action.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)){
                    Toast.makeText(MainActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                }else if (action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK)){
                    Toast.makeText(MainActivity.this,"key权限成功",Toast.LENGTH_SHORT).show();
                }else if(action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)){}
                    Toast.makeText(MainActivity.this,"key权限失败",Toast.LENGTH_SHORT).show();
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);//网络错误
        filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);//权限检查ok
        filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);//权限检查错误
        registerReceiver(receiver,filter);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
        unregisterReceiver(receiver);
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.showfunc:
                if (llfunc.getVisibility()==View.VISIBLE){
                    llfunc.setVisibility(View.GONE);
                }else {
                    llfunc.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.zoomin:
                mapStatusUpdate = MapStatusUpdateFactory.zoomIn();
                mapcontroller.animateMapStatus(mapStatusUpdate);
                break;
            case R.id.zoomout:
                mapStatusUpdate = MapStatusUpdateFactory.zoomOut();
                mapcontroller.animateMapStatus(mapStatusUpdate);
                break;
            case R.id.rotate:
                float rot = mapcontroller.getMapStatus().rotate;
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.rotate(rot+30);
                MapStatus mapsta = builder.build();
                mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapsta);
                mapcontroller.setMapStatus(mapStatusUpdate);
                break;
            case R.id.overlook:
                float overlook = mapcontroller.getMapStatus().overlook;
                builder = new MapStatus.Builder();
                builder.overlook(overlook-5);
                mapsta = builder.build();
                mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapsta);
                mapcontroller.setMapStatus(mapStatusUpdate);
                break;
            case R.id.move:
                mapStatusUpdate = MapStatusUpdateFactory.newLatLng(new LatLng(39.520888,116.715081));
                mapcontroller.animateMapStatus(mapStatusUpdate,600);
                break;
            default:
                break;
        }
    }
}
